package com.hamster.crmpre.web;

import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.pojo.PersonalMessage;
import com.hamster.crmpre.service.MessageQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/26 15:32
 * @description：消息查询控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("messageQuery")
public class MessageQueryController {

    @Autowired
    private MessageQueryService messageQueryService;

    /**
     * 查询个人消息的请求处理
     * @return 个人消息
     */
    @RequestMapping("list")
    public ResponseEntity<List<PersonalMessage>> list(@RequestParam(required = false) String name){
        String dep = LoginInterceptor.getLoginUser().getDepartment();
        if (dep != null){
            if (LoginInterceptor.getLoginUser().getDepartment().split(",")[0].equals("4")) {//如果是财务部的人
                String receiverId = LoginInterceptor.getLoginUser().getUserId();
                String department = LoginInterceptor.getLoginUser().getDepartment();
                department = department.split(",")[0];
                List<PersonalMessage> personalMessageList = messageQueryService.listQueryFinance(receiverId,department,name);
                return ResponseEntity.ok(personalMessageList);
            }else if (LoginInterceptor.getLoginUser().getDepartment().split(",")[1].equals("director")){//如果是服务部 或 事业部 总监
                String receiverId = LoginInterceptor.getLoginUser().getUserId();
                String department = LoginInterceptor.getLoginUser().getDepartment().split(",")[0];
                List<PersonalMessage> personalMessageList = messageQueryService.listQueryFinance(receiverId,department,name);
                return ResponseEntity.ok(personalMessageList);
            } else {//普通 人
                String receiverId = LoginInterceptor.getLoginUser().getUserId();
                List<PersonalMessage> personalMessageList = messageQueryService.list(receiverId,name);
                return ResponseEntity.ok(personalMessageList);
            }
        }
        else {//普通 人
            String receiverId = LoginInterceptor.getLoginUser().getUserId();
            List<PersonalMessage> personalMessageList = messageQueryService.list(receiverId,name);
            return ResponseEntity.ok(personalMessageList);
        }

    }


    /**
     * 查询个人消息的未读消息数
     * @return 个人消息
     */
    @RequestMapping("listCount")
    public ResponseEntity<Integer> listCount(){
        String dep = LoginInterceptor.getLoginUser().getDepartment();
        if (dep != null){
            if (LoginInterceptor.getLoginUser().getDepartment().split(",")[0].equals("4")) {//如果是财务部的人
                String receiverId = LoginInterceptor.getLoginUser().getUserId();
                String department = LoginInterceptor.getLoginUser().getDepartment();
                department = department.split(",")[0];
                Integer count  = messageQueryService.listQueryFinanceCount(receiverId,department);
                return ResponseEntity.ok(count);
            }else if (LoginInterceptor.getLoginUser().getDepartment().split(",")[1].equals("director")){//如果是服务部 或 事业部 总监
                String receiverId = LoginInterceptor.getLoginUser().getUserId();
                String department = LoginInterceptor.getLoginUser().getDepartment().split(",")[0];
                Integer count  = messageQueryService.listQueryFinanceCount(receiverId,department);
                return ResponseEntity.ok(count);
            } else {//普通 人
                String receiverId = LoginInterceptor.getLoginUser().getUserId();
                Integer count = messageQueryService.listCount(receiverId);
                return ResponseEntity.ok(count);
            }
        }
        else {//普通 人
            String receiverId = LoginInterceptor.getLoginUser().getUserId();
            Integer count = messageQueryService.listCount(receiverId);
            return ResponseEntity.ok(count);
        }

    }
}
